package com.xiran.todoself.controller;

import com.xiran.todoself.common.Response;
import com.xiran.todoself.model.dto.LoginDto;
import com.xiran.todoself.model.pojo.User;
import com.xiran.todoself.service.UserService;
import com.xiran.todoself.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@Api(tags = "认证")
@Slf4j
public class AuthController {
    @Resource
    private UserService userService;
    @Resource
    private JwtUtils jwtUtils;

    @PostMapping("/register")
    @ApiOperation("注册")
    public Response<Object> register(@RequestBody LoginDto dto) {
        userService.register(dto.getUsername(), dto.getPassword());
        return Response.success(200, "注册成功啦");
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public Map<String, String> login(@RequestBody LoginDto dto) {
        User u = userService.login(dto.getUsername(), dto.getPassword());
        String token = jwtUtils.generateToken(u.getId());
        return Collections.singletonMap("token", token);
    }

    @GetMapping("/profile")
    @ApiOperation("获得用户信息")
    public String userya(String des) {
        log.info("获取用户信息");
        return userService.getMainMessage(des);

    }

    @PostMapping("/change")
    @ApiOperation("更改密码")
    public Response<String> change(@RequestBody LoginDto dto){
        log.info("准备开始切换账号信息啦");


        userService.change(dto);
        return Response.success(200,"更改账号信息成功啦");
    }
}
